﻿using Microsoft.AspNetCore.Http;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using SystemHYS.API.Application.command.GualityGroupCommand;
using SystemHYS.Domian;
using SystemHYS.API.DTOS;
using SystemHYS.API.Application.command.DeliveryCommand;

namespace SystemHYS.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeliveryController : ControllerBase
    {
        IMediator mediator;
        public DeliveryController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 获取出库信息
        /// </summary>
        /// <param name="command">查询条件</param>
        /// <returns>返回出库信息</returns>
        [HttpGet]
        public async Task<Apiresult<List<Delivery>>> GetDelivery([FromQuery]DeliveryShowCommand command)
        {
            Apiresult<List<Delivery>> apiresult = new Apiresult<List<Delivery>>();
            try
            {
                apiresult.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                apiresult.Code = -1;
                apiresult.Message = ex.Message;
                throw;
            }
            return apiresult;
        }

        /// <summary>
        /// 添加出库信息
        /// </summary>
        /// <param name="command">出库信息</param>
        /// <returns>返回受影响信息</returns>
        [HttpPost]
        public async Task<Apiresult<int>> AddDelivery(DeliveryCreateCommand command)
        {
            Apiresult<int> apiresult = new Apiresult<int>();
            try
            {
                apiresult.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                apiresult.Code = -1;
                apiresult.Message = ex.Message;
                throw;
            }
            return apiresult;
        }
        /// <summary>
        /// 删除出库信息
        /// </summary>
        /// <param name="command">出库id</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<Apiresult<int>> DeleteDelivery(DeliveryDeleteCommand command)
        {
            Apiresult<int> apiresult = new Apiresult<int>();
            try
            {
                apiresult.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                apiresult.Code = -1;
                apiresult.Message = ex.Message;
                throw;
            }
            return apiresult;
        }
        /// <summary>
        /// 查看出库信息详情
        /// </summary>
        /// <param name="command">出库id</param>
        /// <returns>返回出库信息</returns>
        [HttpGet]
        public async Task<Apiresult<Delivery>> ModelDelivery([FromQuery] DeliveryModelCommand command)
        {
            Apiresult<Delivery> apiresult = new Apiresult<Delivery>();
            try
            {
                apiresult.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                apiresult.Code = -1;
                apiresult.Message = ex.Message;
                throw;
            }
            return apiresult;
        }
        /// <summary>
        /// 修改出库信息
        /// </summary>
        /// <param name="command">出库信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<Apiresult<int>> UpdateDelivery(DeliveryUpdateCommand command)
        {
            Apiresult<int> apiresult = new Apiresult<int>();
            try
            {
                apiresult.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                apiresult.Code = -1;
                apiresult.Message = ex.Message;
                throw;
            }
            return apiresult;
        }
        /// <summary>
        /// 成品状态出库
        /// </summary>
        /// <param name="command">出库id</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<Apiresult<int>> UpdateStateDelivery(DeliveryUpdateStateCommand command)
        {
            Apiresult<int> apiresult = new Apiresult<int>();
            try
            {
                apiresult.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                apiresult.Code = -1;
                apiresult.Message = ex.Message;
                throw;
            }
            return apiresult;
        }
    }
}
